import 'package:flutter/material.dart';
import '../models/ddl_item.dart';
import '../app_data_manager.dart';

class DDLDetailCard extends StatelessWidget {
  final DDLItem item;
  final bool isInRecycleBin;
  final VoidCallback onTap;
  final Widget? dragFeedback;
  final Widget? dragChild;

  const DDLDetailCard({
    super.key,
    required this.item,
    this.isInRecycleBin = false,
    required this.onTap,
    this.dragFeedback,
    this.dragChild,
  });

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: onTap,
      child: Card(
        margin: const EdgeInsets.all(8),
        child: Padding(
          padding: const EdgeInsets.all(16),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                children: [
                  Expanded(
                    child: Text(
                      item.title,
                      style: const TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                  if (isInRecycleBin)
                    Container(
                      padding: const EdgeInsets.symmetric(
                        horizontal: 8,
                        vertical: 4,
                      ),
                      decoration: BoxDecoration(
                        color: item.isCompleted
                            ? Colors.green[100]
                            : item.isAbandoned
                                ? Colors.red[100]
                                : Colors.orange[100],
                        borderRadius: BorderRadius.circular(12),
                      ),
                      child: Text(
                        item.isCompleted
                            ? '已完成'
                            : item.isAbandoned
                                ? '已放弃'
                                : '已取消',
                        style: TextStyle(
                          color: item.isCompleted
                              ? Colors.green[700]
                              : item.isAbandoned
                                  ? Colors.red[700]
                                  : Colors.orange[700],
                          fontSize: 12,
                        ),
                      ),
                    ),
                ],
              ),
              const SizedBox(height: 8),
              Text(
                '截止日期: ${_formatDateTime(item.deadline)}',
                style: TextStyle(color: Colors.grey[600]),
              ),
              if (item.startDate != null) ...[
                const SizedBox(height: 4),
                Text(
                  '开始日期: ${_formatDateTime(item.startDate!)}',
                  style: TextStyle(color: Colors.grey[600]),
                ),
              ],
              const SizedBox(height: 4),
              Text(
                _getRemainingTime(item.deadline),
                style: TextStyle(
                  color: _getTimeColor(item.deadline),
                  fontWeight: FontWeight.bold,
                ),
              ),
              if (item.description != null && item.description!.isNotEmpty) ...[
                // const SizedBox(height: 8),
                // Text(
                //   item.description!,
                //   maxLines: 2,
                //   overflow: TextOverflow.ellipsis,
                //   style: TextStyle(color: Colors.grey[600]),
                // ),
              ],
            ],
          ),
        ),
      ),
    );
  }

  String _formatDateTime(DateTime? dateTime) {
    if (dateTime == null) return '未设置';
    return '${dateTime.month}月${dateTime.day}号${dateTime.hour}时${dateTime.minute}分';
  }

  String _getRemainingTime(DateTime? deadline) {
    if (deadline == null) return '未设置';
    final now = DateTime.now();
    final difference = deadline.difference(now);
    if (difference.isNegative) return '已超时';
    final hours = difference.inHours;
    final minutes = difference.inMinutes % 60;
    if (hours > 24) {
      final days = hours ~/ 24;
      final remainingHours = hours % 24;
      return '还剩 $days 天 $remainingHours 小时';
    } else if (hours > 0) {
      return '还剩 $hours 小时 $minutes 分钟';
    } else {
      return '还剩 $minutes 分钟';
    }
  }

  Color _getTimeColor(DateTime? deadline) {
    if (deadline == null) return Colors.grey;
    final now = DateTime.now();
    final difference = deadline.difference(now);
    if (difference.isNegative) return Colors.red;
    if (difference.inHours <= 24) return Colors.orange;
    return Colors.green;
  }
} 